package cn.calm.commons.action;

import java.util.List;

import org.apache.log4j.Logger;

import cn.calm.commons.entity.BaseEntity;
import cn.calm.commons.exception.DeskException;
import cn.calm.commons.service.IBaseService;

public abstract class CurdAction<T extends BaseEntity, S extends IBaseService<T>>
		extends BaseAction<T> 
//implements ServiceAware<S> 
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger log = Logger.getLogger(CurdAction.class);

	protected List<T> list;

	protected S service;
	
	/**
	 * 根据实体类t检索所有数据
	 * 
	 * @return 执行结果
	 */
	public String listAll() {
		log.debug("listAll start");
		list = service.list(t);
		log.debug("listAll end");
		return "listAll";
	}

	/**
	 * 根据实体t检索分页数据
	 * 
	 * @return 执行结果
	 */
	public String listPage() {
		log.debug("listPage start");
		pageMode = service.listPage(pageMode, t);
		log.debug("listPage end");
		return "listPage";
	}

	/**
	 * 将t添加到数据库
	 * 
	 * @return 执行结果
	 */
	public String add() {
		log.debug("add start");
		service.add(t);
		addActionMessage("IM0001", t);
		log.debug("add end");
		return "add";
	}

	/**
	 * 根据实体t查询数据
	 * 
	 * @return 执行结果
	 */
	public String load() {
		log.debug("load start");
		t = service.load(t);
		log.debug("load end");
		return "load";
	}

	/**
	 * 
	 * 更新数据
	 * 
	 * @return 执行结果
	 */
	public String update() {
		log.debug("update start");
		try {
			service.update(t);
			addActionMessage("IM0002", t);
		} catch (DeskException e) {
			addActionError(e.getMessageInfo());
		}
		log.debug("update end");
		return "update";
	}

	/**
	 * 
	 * 删除数据
	 * 
	 * @return 执行结果
	 */
	public String delete() {
		log.debug("delete start");
		service.delete(t);
		addActionMessage("IM0003", t);
		log.debug("delete end");
		return "delete";
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

//	@Override
//	public void setServices(List<S> services) {
//		if (services != null && services.size() > 0) {
//			service = services.get(0);
//		}
//	}
}
